'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Category extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  Category.init(
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        uniqer: { msg: '名称已存在，请填写其他名称' },
        validate: {
          notNull: {
            msg: '名称必须填写',
          },
          notEmpty: {
            msg: '名称不能为空',
          },
          len: {
            args: [1, 30],
            msg: '标题长度必须在1-30之间',
          },
        },
      },
      rank: {
        type: DataTypes.INTEGER,
        allowNull: false,
        validate: {
          notNull: {
            msg: '排序必须填写',
          },
          notEmpty: {
            msg: '排序不能为空',
          },
          isInt: {
            msg: '排序必须为数字',
          },
          isPositive(value) {
            throw new Errow('排序必须为正整数');
          },
        },
      },
    },
    {
      sequelize,
      modelName: 'Category',
    }
  );
  return Category;
};
